/***
This do-file creates creates a bar chart of stimulus effect sizes.
***/

*-------------------------------------------------------------------------------
* Setup
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Policy"

* Set seed
set seed 1280

*-------------------------------------------------------------------------------
**# 1. Load and process data
*-------------------------------------------------------------------------------

* Load dataset
project, uses("${root}/data/derived/Policy/stimulus_did_dataset.dta")
use "${root}/data/derived/Policy/stimulus_did_dataset.dta", clear
gisid round income_q date

* Empty dataset for us to store the regression estimates
preserve

keep round income_q
gduplicates drop

* Separate rows for detrended and original spending values
expand 2, gen(dup)
gen specification = "detrended" if dup == 0
replace specification = "original" if dup == 1
drop dup

* Separate rows for point estimate and standard errors
expand 2, gen(dup)
gen estimate = "beta" if dup == 0
replace estimate = "se" if dup == 1
drop dup

gen first_five_pp = .              // percentage point effect in first five days
gen after_first_five_pp = .        // percentage point effect after first five days
gen combined_pp = .                // percentage point effect (31 days after stimulus)
gen combined_dollar = .            // dollar effect (31 days after stimulus, cumulative)
gen combined_dollar_rescaled = .   // dollar effect per $1200 (31 days after stimulus, cumulative)

sort round income_q specification estimate

tempfile estimates
save `estimates', replace
restore

*-------------------------------------------------------------------------------
**# 2. Diff-in-Diff
*-------------------------------------------------------------------------------

/*
Regression spec: separate regressions for each stimulus round and income quartile
Dependent variable: spending changes normed to base January 2019 spending, residualized on DoW FEs,
					main specification detrends (see Policy - Create stimulus datasets.do) and robustness check does not
Treatment variables: indicator for post-covid (treat),
                     interaction between indicator for post-covid and first five days after stimulus/stimulus control (first_five),
					 interaction between indicator for post-covid and subsequent days after stimulus/stimulus control (after_first_five)
*/

foreach round in april january march {
	forval income_q = 1/4 {

		* Base spending levels per stimulus recipient in Jan 2019
		sum spend_pp if round == "`round'" & income_q == `income_q'
		local spend_pp = r(mean)

		foreach depvar in dow_resid_2019_notrend dow_resid_2019 {

			if "`depvar'" == "dow_resid_2019_notrend" local spec = "detrended"
			else if "`depvar'" == "dow_resid_2019" local spec = "original"

			* Diff-in-Diff
			reg `depvar' i.treat##i.first_five i.treat##i.after_first_five if round == "`round'" & income_q == `income_q' & analysis_window == 1, r

			* 24 days pre-, 25 days post- for April; 11 days pre- and 16 days post- for January; 21 days pre-, 25 days post- for March
			if "`round'" == "april" assert e(N) == (24 + 25) * 2
			else if "`round'" == "january" assert e(N) == (11 + 16) * 2
			else if "`round'" == "march" assert e(N) == (21 + 25) * 2

			* Record regression estimates
			preserve

			use `estimates', clear

			* Percentage point effect in first five days
			replace first_five_pp = _b[1.treat#1.first_five] * 100 if round == "`round'" & income_q == `income_q' & specification == "`spec'" & estimate == "beta"
			replace first_five_pp = _se[1.treat#1.first_five] * 100 if round == "`round'" & income_q == `income_q' & specification == "`spec'" & estimate == "se"

			* Percentage point effect after first five days
			replace after_first_five_pp = _b[1.treat#1.after_first_five] * 100 if round == "`round'" & income_q == `income_q' & specification == "`spec'" & estimate == "beta"
			replace after_first_five_pp = _se[1.treat#1.after_first_five] * 100 if round == "`round'" & income_q == `income_q' & specification == "`spec'" & estimate == "se"

			* Percentage point effect (31 days after stimulus)
			lincom (1.treat#1.first_five * 5 + 1.treat#1.after_first_five * 26) / 31
			replace combined_pp = r(estimate) * 100 if round == "`round'" & income_q == `income_q' & specification == "`spec'" & estimate == "beta"
			replace combined_pp = r(se) * 100 if round == "`round'" & income_q == `income_q' & specification == "`spec'" & estimate == "se"

			* Dollar effect (31 days after stimulus, cumulative)
			lincom (1.treat#1.first_five * 5 + 1.treat#1.after_first_five * 26) * `spend_pp'
			replace combined_dollar = r(estimate) if round == "`round'" & income_q == `income_q' & specification == "`spec'" & estimate == "beta"
			replace combined_dollar = r(se) if round == "`round'" & income_q == `income_q' & specification == "`spec'" & estimate == "se"

			* Dollar estimate per $1200 (31 days after stimulus, cumulative)
			if "`round'" == "april" replace combined_dollar_rescaled = combined_dollar * 1200 / 1200 if round == "`round'" & specification == "`spec'" & income_q == `income_q'
			if "`round'" == "january" replace combined_dollar_rescaled = combined_dollar * 1200 / 600 if round == "`round'" & specification == "`spec'" & income_q == `income_q'
			if "`round'" == "march" replace combined_dollar_rescaled = combined_dollar * 1200 / 1400 if round == "`round'" & specification == "`spec'" & income_q == `income_q'

			save `estimates', replace
			restore
		}
	}
}

*-------------------------------------------------------------------------------
**# 3. Plot bar chart of estimates
*-------------------------------------------------------------------------------

use `estimates', clear
save "${root}/data/derived/stimulus_estimates.dta", replace
project, creates("${root}/data/derived/stimulus_estimates.dta")

*-------------------------------------------------------------------------------
* Plot bar chart of effects in dollars
*-------------------------------------------------------------------------------
* Create positions
gen pos = income_q - 0.5 + 0.17 if round == "april"
replace pos = income_q - 0.5 + 0.34 if round == "january"
replace pos = income_q - 0.5 + 0.51 if round == "march"

* Make chart showing effect in dollars per recipient
gen display_results = round(combined_dollar_rescaled, 1) if estimate == "beta"
rename display_results display_results_num
tostring(display_results_num), gen(display_results)
replace display_results = "$" + display_results if display_results_num >= 0
replace display_results = "-$" + subinstr(display_results, "-", "", .) if display_results_num < 0

* To calculate pvals from permutation tests - difference in dollar estimates between stim 1 vs. 2 and 3
preserve

foreach income_q in 1 4 {
	foreach spec in "detrended" "original" {
		sum combined_dollar_rescaled if round == "april" & income_q == `income_q' & specification == "`spec'" & estimate == "beta"
		assert r(N) == 1
		loc stim1_estimate = r(mean)

		sum combined_dollar_rescaled if round == "january" & income_q == `income_q' & specification == "`spec'" & estimate == "beta"
		assert r(N) == 1
		loc stim2_estimate = r(mean)

		sum combined_dollar_rescaled if round == "march" & income_q == `income_q' & specification == "`spec'" & estimate == "beta"
		assert r(N) == 1
		loc stim3_estimate = r(mean)

		local stim1v2_q`income_q'_`spec' = `stim1_estimate' - `stim2_estimate'
		local stim1v3_q`income_q'_`spec' = `stim1_estimate' - `stim3_estimate'
		local stim2v3_q`income_q'_`spec' = `stim2_estimate' - `stim3_estimate'
	}
}

clear
set obs 4
gen income_q = .
gen specification = ""
gen stim1v2 = .
gen stim1v3 = .
gen stim2v3 = .

replace income_q = 1 in 1
replace specification = "detrended" in 1
replace stim1v2 = `stim1v2_q1_detrended' in 1
replace stim1v3 = `stim1v3_q1_detrended' in 1
replace stim2v3 = `stim2v3_q1_detrended' in 1

replace income_q = 4 in 2
replace specification = "detrended" in 2
replace stim1v2 = `stim1v2_q4_detrended' in 2
replace stim1v3 = `stim1v3_q4_detrended' in 2
replace stim2v3 = `stim2v3_q4_detrended' in 2

replace income_q = 1 in 3
replace specification = "original" in 3
replace stim1v2 = `stim1v2_q1_original' in 3
replace stim1v3 = `stim1v3_q1_original' in 3
replace stim2v3 = `stim2v3_q1_original' in 3

replace income_q = 4 in 4
replace specification = "original" in 4
replace stim1v2 = `stim1v2_q4_original' in 4
replace stim1v3 = `stim1v3_q4_original' in 4
replace stim2v3 = `stim2v3_q4_original' in 4

save "${root}/data/derived/stimulus_dollar_estimate_diff.dta", replace
project, creates("${root}/data/derived/stimulus_dollar_estimate_diff.dta")

restore

* Permutation tests
preserve

project, do("${root}/code/figs/Policy/Policy - Permutation Stim Q1 and Q4.do")

* Import pvals from permutations
project, uses("${root}/results/stim_pvals.xlsx")
import excel "${root}/results/stim_pvals.xlsx", sheet(stim_pvals) firstrow clear

foreach income_q in 1 4 {
	foreach spec in "detrended" "original" {

		* Stim 1 v 2
		sum pval_stim1v2 if income_q == `income_q' & specification == "`spec'"
		assert r(N) == 1

		if r(mean) > 0 {
			local pval_stim1v2_q`income_q'_`spec': di %04.3f r(mean)
			local pval_stim1v2_q`income_q'_`spec' "= `pval_stim1v2_q`income_q'_`spec''"
		}
		else if r(mean) == 0 local pval_stim1v2_q`income_q'_`spec' "< 0.001"

		* Stim 1 v 3
		sum pval_stim1v3 if income_q == `income_q' & specification == "`spec'"
		assert r(N) == 1

		if r(mean) > 0 {
			local pval_stim1v3_q`income_q'_`spec': di %04.3f r(mean)
			local pval_stim1v3_q`income_q'_`spec' "= `pval_stim1v3_q`income_q'_`spec''"
		}
		else if r(mean) == 0 local pval_stim1v3_q`income_q'_`spec' "< 0.001"

		* Stim 2 v 3
		sum pval_stim2v3 if income_q == `income_q' & specification == "`spec'"
		assert r(N) == 1

		if r(mean) > 0 {
			local pval_stim2v3_q`income_q'_`spec': di %04.3f r(mean)
			local pval_stim2v3_q`income_q'_`spec' "= `pval_stim2v3_q`income_q'_`spec''"
		}
		else if r(mean) == 0 local pval_stim2v3_q`income_q'_`spec' "< 0.001"
	}
}

restore

* Bar graph, detrended values
preserve

keep if specification == "detrended" & estimate == "beta"
gisid round income_q

tw ///
	(bar combined_dollar_rescaled pos if round == "april" & inrange(income_q, 1, 4), barwidth(0.17) color(oi1)) ///
	(bar combined_dollar_rescaled pos if round == "january" & inrange(income_q, 1, 4), barwidth(0.17) color(oi2)) ///
	(bar combined_dollar_rescaled pos if round == "march" & inrange(income_q, 1, 4), barwidth(0.17) color(oi3)) ///
	///
	(scatter combined_dollar_rescaled pos if inrange(income_q, 1, 4) & combined_dollar_rescaled >= 0, ///
		msymbol(i) mlabel(display_results) mlabpos(12) mlabgap(1) mlabsize(small) mlabcolor(gs8)) ///
	(scatter combined_dollar_rescaled pos if inrange(income_q, 1, 4) & combined_dollar_rescaled < 0, ///
		msymbol(i) mlabel(display_results) mlabpos(6) mlabgap(0.5) mlabsize(small) mlabcolor(gs8)) ///
	, ///
	xlabel(`=1 - 0.5 + 0.34' "{bf:Bottom Quartile}" `=2 - 0.5 + 0.34' "{bf:Second Quartile}" `=3 - 0.5 + 0.34' "{bf:Third Quartile}" `=4 - 0.5 + 0.34' "{bf:Top Quartile}", labsize(small) notick) ///
	xtick(none) ///
	ytitle("Estimated 1-Month Spending Impact" "Per $1200 of Stimulus") ///
	legend(order(1 "April 2020" "(CARES Act)" 2 "January 2021" "(COVID-Related" "Tax Relief Act)" 3 "March 2021" "(American Rescue" "Plan Act)") row(1) size(vsmall)) ///
	title("") ///
	ylab(0 "$0" 200 "$200" 400 "$400" 600 "$600" 800 "$800", nogrid) ///
	yline(0, lcolor(gs8)) ///
	xtitle("") ///
	xsc(ra(0.7 3.3) lc(white)) ///
	ysc(ra(0 900)) ///
	///
	text(535 0.75 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(570 0.78 "p `pval_stim1v2_q1_detrended'", color(gs8) size(3)) ///
	text(605 0.85 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(640 0.88 "p `pval_stim1v3_q1_detrended'", color(gs8) size(3)) ///
	text(360 0.91 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(395 0.94 "p `pval_stim2v3_q1_detrended'", color(gs8) size(3)) ///
	///
	text(815 3.75 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(850 3.78 "p `pval_stim1v2_q4_detrended'", color(gs8) size(3)) ///
	text(885 3.85 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(920 3.88 "p `pval_stim1v3_q4_detrended'", color(gs8) size(3)) ///
	text(150 3.91 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(185 3.94 "p `pval_stim2v3_q4_detrended'", color(gs8) size(3))

	graph export "${root}/results/Policy/Stimulus bar chart by round and income quartile (dollars).svg", replace
	project, creates("${root}/results/Policy/Stimulus bar chart by round and income quartile (dollars).svg")

* Black and white version for QJE
tw ///
	(bar combined_dollar_rescaled pos if round == "april" & inrange(income_q, 1, 4), barwidth(0.17) color(gs0)) ///
	(bar combined_dollar_rescaled pos if round == "january" & inrange(income_q, 1, 4), barwidth(0.17) color(gs6)) ///
	(bar combined_dollar_rescaled pos if round == "march" & inrange(income_q, 1, 4), barwidth(0.17) color(gs12)) ///
	///
	(scatter combined_dollar_rescaled pos if inrange(income_q, 1, 4) & combined_dollar_rescaled >= 0, ///
		msymbol(i) mlabel(display_results) mlabpos(12) mlabgap(1) mlabsize(small) mlabcolor(gs8)) ///
	(scatter combined_dollar_rescaled pos if inrange(income_q, 1, 4) & combined_dollar_rescaled < 0, ///
		msymbol(i) mlabel(display_results) mlabpos(6) mlabgap(0.5) mlabsize(small) mlabcolor(gs8)) ///
	, ///
	xlabel(`=1 - 0.5 + 0.34' "{bf:Bottom Quartile}" `=2 - 0.5 + 0.34' "{bf:Second Quartile}" `=3 - 0.5 + 0.34' "{bf:Third Quartile}" `=4 - 0.5 + 0.34' "{bf:Top Quartile}", labsize(small) notick) ///
	xtick(none) ///
	ytitle("Estimated 1-Month Spending Impact" "Per $1200 of Stimulus") ///
	legend(order(1 "April 2020" "(CARES Act)" 2 "January 2021" "(COVID-Related" "Tax Relief Act)" 3 "March 2021" "(American Rescue" "Plan Act)") row(1) size(vsmall)) ///
	title("") ///
	ylab(0 "$0" 200 "$200" 400 "$400" 600 "$600" 800 "$800", nogrid) ///
	yline(0, lcolor(gs8)) ///
	xtitle("") ///
	xsc(ra(0.7 3.3) lc(white)) ///
	ysc(ra(0 900)) ///
	///
	text(535 0.75 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(570 0.78 "p `pval_stim1v2_q1_detrended'", color(gs8) size(3)) ///
	text(605 0.85 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(640 0.88 "p `pval_stim1v3_q1_detrended'", color(gs8) size(3)) ///
	text(360 0.91 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(395 0.94 "p `pval_stim2v3_q1_detrended'", color(gs8) size(3)) ///
	///
	text(815 3.75 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(850 3.78 "p `pval_stim1v2_q4_detrended'", color(gs8) size(3)) ///
	text(885 3.85 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(920 3.88 "p `pval_stim1v3_q4_detrended'", color(gs8) size(3)) ///
	text(150 3.91 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(185 3.94 "p `pval_stim2v3_q4_detrended'", color(gs8) size(3))

	graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_8.svg", replace
	project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_8.svg")

restore

* Bar graph, original values
preserve

keep if specification == "original" & estimate == "beta"
gisid round income_q

tw ///
	(bar combined_dollar_rescaled pos if round == "april" & inrange(income_q, 1, 4), barwidth(0.17) color(oi1)) ///
	(bar combined_dollar_rescaled pos if round == "january" & inrange(income_q, 1, 4), barwidth(0.17) color(oi2)) ///
	(bar combined_dollar_rescaled pos if round == "march" & inrange(income_q, 1, 4), barwidth(0.17) color(oi3)) ///
	///
	(scatter combined_dollar_rescaled pos if inrange(income_q, 1, 4) & combined_dollar_rescaled >= 0, ///
		msymbol(i) mlabel(display_results) mlabpos(12) mlabgap(1) mlabsize(small) mlabcolor(gs8)) ///
	(scatter combined_dollar_rescaled pos if inrange(income_q, 1, 4) & combined_dollar_rescaled < 0, ///
		msymbol(i) mlabel(display_results) mlabpos(6) mlabgap(0.5) mlabsize(small) mlabcolor(gs8)) ///
	, ///
	xlabel(`=1 - 0.5 + 0.34' "{bf:Bottom Quartile}" `=2 - 0.5 + 0.34' "{bf:Second Quartile}" `=3 - 0.5 + 0.34' "{bf:Third Quartile}" `=4 - 0.5 + 0.34' "{bf:Top Quartile}", labsize(small) notick) ///
	xtick(none) ///
	ytitle("Estimated 1-Month Spending Impact" "Per $1200 of Stimulus") ///
	legend(order(1 "April 2020" "(CARES Act)" 2 "January 2021" "(COVID-Related" "Tax Relief Act)" 3 "March 2021" "(American Rescue" "Plan Act)") row(1) size(vsmall)) ///
	title("") ///
	ylab(0 "$0" 200 "$200" 400 "$400" 600 "$600" 800 "$800", nogrid) ///
	yline(0, lcolor(gs8)) ///
	xtitle("") ///
	xsc(ra(0.7 3.3) lc(white)) ///
	ysc(ra(0 800)) ///
	///
	text(500 0.75 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(525 0.78 "p `pval_stim1v2_q1_original'", color(gs8) size(3)) ///
	text(560 0.85 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(585 0.88 "p `pval_stim1v3_q1_original'", color(gs8) size(3)) ///
	text(420 0.91 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(445 0.94 "p `pval_stim2v3_q1_original'", color(gs8) size(3)) ///
	///
	text(700 3.75 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(725 3.78 "p `pval_stim1v2_q4_original'", color(gs8) size(3)) ///
	text(760 3.85 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AA")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(785 3.88 "p `pval_stim1v3_q4_original'", color(gs8) size(3)) ///
	text(350 3.91 "`=ustrunescape("\u23AB")'" "`=ustrunescape("\u23AC")'" "`=ustrunescape("\u23AD")'", orientation(vertical) color(gs8)) ///
	text(375 3.94 "p `pval_stim2v3_q4_original'", color(gs8) size(3))

	graph export "${root}/results/Policy/Stimulus bar chart by round and income quartile (dollars) original.svg", replace
	project, creates("${root}/results/Policy/Stimulus bar chart by round and income quartile (dollars) original.svg")

restore

*-------------------------------------------------------------------------------
**# 4. Regression table
*-------------------------------------------------------------------------------

preserve

* Only need table for detrended values
keep if specification == "detrended"

* Format
foreach var in first_five_pp after_first_five_pp combined_pp combined_dollar combined_dollar_rescaled {

	* Store as rounded in double format
	gen double temp = round(`var', 0.01)
	drop `var'
	rename temp `var'

	* Convert to string
	gen temp = string(`var')
	drop `var'
	rename temp `var'

	* Ensure there is a leading zero
	replace `var' = "0" + `var' if strpos(`var', ".") == 1
	replace `var' = subinstr(`var', "-", "-0", .) if strpos(`var', "-") == 1 & strpos(`var', ".") == 2

	* Ensure there are two spaces after the decimal point
	replace `var' = `var' + "0" if length(`var') - strpos(`var', ".") == 1
	replace `var' = `var' + ".00" if !strpos(`var', ".")

	* Put SEs in parentheses
	replace `var' = "(" + `var' + ")" if estimate == "se"

	* Decimal places for zero values
	replace `var' = "0.000" if "`var'" == "0"
}

* Order
keep round income_q estimate first_five_pp after_first_five_pp combined_pp combined_dollar combined_dollar_rescaled
order round income_q estimate first_five_pp after_first_five_pp combined_pp combined_dollar combined_dollar_rescaled

* Two blank lines between stimulus rounds for formatting
assert _n == 8 if round == "april" & income_q == 4 & estimate == "se"
insobs 2, after(8)
assert _n == 16 + 2 if round == "january" & income_q == 4 & estimate == "se"
insobs 2, after(18)

* Export
export excel "${root}/results/new_app_table_6.xlsx", sheet(new_app_table_6, replace)
project, creates("${root}/results/new_app_table_6.xlsx")

restore

*-------------------------------------------------------------------------------
**# 5. Stats
*-------------------------------------------------------------------------------

* Only need detrended values
keep if specification == "detrended"

cap erase "${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml"

* Percentage difference per $1200 dollar estimate in Stim 1 vs. Stim 2
foreach income_q in 1 4 {

	sum combined_dollar_rescaled if round == "april" & income_q == `income_q' & estimate == "beta"
	assert r(N) == 1
	local beta_q`income_q'_apr = r(mean)

	sum combined_dollar_rescaled if round == "january" & income_q == `income_q' & estimate == "beta"
	assert r(N) == 1
	local beta_q`income_q'_jan = r(mean)

	local pct_diff_q`income_q'_apr_jan: di %2.0f abs(`beta_q`income_q'_jan' / `beta_q`income_q'_apr' - 1) * 100

	yamlout using "${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml", ///
		key("pct_diff_q`income_q'_apr_jan") ///
		comment("Diff in per 1200 dollar estimate of stimulus effect, Q`income_q'") ///
		value(`pct_diff_q`income_q'_apr_jan') fmt(%2.0f)
}

* Estimates
foreach round in april january march {
	local round_cap = strproper("`round'")

	foreach income_q in 1 4 {

		* Percentage point estimate of each stimulus round
		sum combined_pp if round == "`round'" & income_q == `income_q' & estimate == "beta"
		assert r(N) == 1
		if abs(r(mean)) >= 1 {
			local mean`income_q'_str: di %2.0f r(mean)

			yamlout using "${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml", ///
				key("effect_`round_cap'_q`income_q'_point") ///
				comment("Effects of stimulus round `round' for Q`income_q' household (percentage point)") ///
				value(`mean`income_q'_str') fmt(%2.0f)
		}

		else {
			local mean`income_q'_str: di %02.1f r(mean)

			yamlout using "${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml", ///
				key("effect_`round_cap'_q`income_q'_point") ///
				comment("Effects of stimulus round `round' for Q`income_q' household (percentage point)") ///
				value(`mean`income_q'_str') fmt(%02.1f)
		}

		* Percentage point estimate (std. err) of each stimulus round
		sum combined_pp if round == "`round'" & income_q == `income_q' & estimate == "se"
		assert r(N) == 1
		local mean`income_q'_str: di %3.2f r(mean)

		yamlout using "${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml", ///
			key("effect_`round_cap'_q`income_q'_se") ///
			comment("Effects of stimulus round `round' for Q`income_q' household (percentage point se)") ///
			value(`mean`income_q'_str') fmt(%3.2f)

		* $1200 dollar estimate of each stimulus round
		sum combined_dollar_rescaled if round == "`round'" & income_q == `income_q' & estimate == "beta"
		assert r(N) == 1
		local mean`income_q'_str = cond(r(mean) >= 0, "$", "-$") + strofreal(abs(r(mean)), "%5.0f")

		yamlout using "${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml", ///
			key("effect_`round_cap'_q`income_q'_dollars") ///
			comment("Effects of stimulus round `round' for Q`income_q' household (dollars)") ///
			value(`mean`income_q'_str')
	}
}

* Mean MPC, pooling stimuli and income quartiles
gen mpc = combined_dollar_rescaled / 1200
sum mpc if estimate == "beta"
assert r(N) == 4 * 3
local mean_mpc = r(mean)

* Impact on spending implied by mean MPC using a standard Keynesian model
local keynesian_impact = round(`mean_mpc' / (1 - `mean_mpc'), 0.001)

yamlout using "${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml", ///
	key("mean_mpc") ///
	comment("Mean MPC") ///
	value(`mean_mpc') fmt(%04.3f)

yamlout using "${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml", ///
	key("keynesian_impact") ///
	comment("Impact on spending implied by mean MPC") ///
	value(`keynesian_impact') fmt(%04.3f)

project, creates("${root}/results/paper numbers/Policy/Stimulus Effects Size.yaml")
